<?php

namespace app\index\controller;

use app\common\controller\HomeBase;
use app\common\model\Article;
use app\common\model\Opinion;
use app\common\model\Site;
use app\common\model\FriendlyLink;
use app\common\model\AizhanApi;
use app\common\model\User;
use Captcha\Captcha;
use think\Cache;
use think\Config;
use think\Request;


class Page extends HomeBase
{
    protected $site_config;

    public function _initialize()
    {
        parent::_initialize();

        $this->siteConfig = Cache::get('site_config');

        $action = Request::instance()->action();
        $this->assign('action', $action);
    }

    public function captcha()
    {
        $m = new Captcha(Config::get('captcha'));

        $img = $m->entry();
        return $img;
    }

    public function index()
    {


        return view();
    }
    public function top()
    {
        //前20赞
        $topTg=(new User())->where('status',1)->order('posted_num','desc')->field('posted_num as total,uuid,nickname,avatar')->limit(20)->select();

        $topZan=(new User())->where('status',1)->order('praise_num','desc')->field('praise_num as total,uuid,nickname,avatar')->limit(20)->select();

        $topView=(new Article())->alias('a')->join('user b','a.user_id=b.id')->where('a.status',1)->group('user_id')->field('sum(total_view) as total,b.uuid,b.nickname,b.avatar')->order('total_view','desc')->limit(20)->select();
        $this->assign('topTg', $topTg);
        $this->assign('topZan', $topZan);
        $this->assign('topView', $topView);
        return view();
    }

    public function ad()
    {
        return view();
    }


    public function send()
    {
        if (request()->isPost()) {
            $params = $this->request->param();
            $validateResult = $this->validate($params, 'Site');
            if ($validateResult !== true) {
                return json(array('code' => 2, 'msg' => $validateResult, 'data' => []));
            }
            $_data = $params['data'];
            if (!captcha_check($_data['captcha'])) {
                return json(array('code' => 2, 'msg' => '验证码错误', 'data' => []));
            }
            //检测一天只能提交三次；
            $ip = $this->request->ip();
            $domain = extractDomainFromUrl($_data['link']);
            if (!$domain) {
                return json(array('code' => 2, 'msg' => '网站域名规则不合法，请单独联系站长QQ人工处理吧！', 'data' => []));
            }
            if (Site::get(['domain' => $domain])) {
                return json(array('code' => 2, 'msg' => '该站点已经提交过了，请不要重复提交哦！', 'data' => []));
            }
            $configPr = $this->siteConfig['apply_friendly_baidu_pr'];
            $prRes = Site::aizhanInfo($domain);
            if (@$prRes['data']) {
                if (empty($prRes['data'])) {
                    return json(array('code' => 2, 'msg' => '我太难了，居然在爱站上没抓取到您的站点，暂时无法收录，或者您也可以联系站长QQ人工收录一下！', 'data' => []));
                } else {
                    $seoData = $prRes['data'];
                    if ($seoData['baidu_pr'] < $configPr && $seoData['baidu_mpr'] < $configPr) {
                        return json(array('code' => 2, 'msg' => '你提交网站的爱站百度权重情况：PC端是' . $seoData['baidu_pr'] . '手机端是' . $seoData['baidu_mpr'] . '<br/>暂时不符合我们的收录要求~', 'data' => []));
                    }else{
                        $data['icp_id'] =$seoData['icp_id'];
                        $data['site_short_content'] =$seoData['seo_title'];
                        $data['site_content'] =$seoData['description'];
                        $data['keywords'] =$seoData['keywords'];
                    }
                }
            }
            $where['poster_ip'] = $ip;
            $where['created_at'] = ['>', date('Y-m-d')];
            $count = (New Site())->where($where)->count();
            if ($count >= 3) {
                return json(array('code' => 2, 'msg' => '你今天提交的站点超标了，明天再来吧。亲！', 'data' => []));
            }
            $data['title'] = remove_xss($_data['name']);
            $data['webmaster_qq'] = remove_xss($_data['qq']);
            $data['link'] = remove_xss($_data['link']);
            $data['domain'] = $domain;
            $data['poster_ip'] = $ip;
            Site::create($data);
            return json(array('code' => 1, 'msg' => '你推荐的酷站信息已成功提交，请耐心等待我们Q你并告知结果！', 'data' => []));
        } else {
            return view();
        }
    }

    public function idea()
    {
        if (request()->isPost()) {
            $params = $this->request->param();
            $validateResult = $this->validate($params, 'Opinion');
            if ($validateResult !== true) {
                return json(array('code' => 2, 'msg' => $validateResult, 'data' => []));
            }
            $_data = $params['data'];
            if (!captcha_check($_data['captcha'])) {
                return json(array('code' => 2, 'msg' => '验证码错误', 'data' => []));
            }
            //检测一天只能提交三次；
            $ip = $this->request->ip();
            $content = remove_xss($_data['opinion']);
            $uniquekey = md5($content);
            if (Opinion::get(['uniquekey' => $uniquekey])) {
                return json(array('code' => 2, 'msg' => '该意见已经反馈过了，请不要重复提交！', 'data' => []));
            }
            $where['poster_ip'] = $ip;
            $where['created_at'] = ['>', date('Y-m-d')];
            $count = (New Opinion())->where($where)->count();
            if ($count >= 3) {
                return json(array('code' => 2, 'msg' => '今天你提交的意见是不是有点多？', 'data' => []));
            }
            $data['poster_qq'] = remove_xss($_data['qq']);
            $data['content'] = $content;
            $data['uniquekey'] = $uniquekey;
            $data['poster_ip'] = $ip;
            Opinion::create($data);
            return json(array('code' => 1, 'msg' => '你提交的建议已成功反馈，请耐心等待我们Q你并告知结果！', 'data' => []));
        } else {
            return view();
        }
    }

    public function friendlink()
    {
        if (request()->isPost()) {
            $params = $this->request->param();
            $validateResult = $this->validate($params, 'FriendlyLink');
            if ($validateResult !== true) {
                return json(array('code' => 2, 'msg' => $validateResult, 'data' => []));
            }
            $_data = $params['data'];
            if (!captcha_check($_data['captcha'])) {
                return json(array('code' => 2, 'msg' => '验证码错误', 'data' => []));
            }
            //检测一天只能提交三次；
            $ip = $this->request->ip();
            $domain = extractDomainFromUrl($_data['link']);
            if (!$domain) {
                return json(array('code' => 2, 'msg' => '链接规则不合法，请单独联系站长QQ人工处理吧！', 'data' => []));
            }
            if (FriendlyLink::get(['domain' => $domain])) {
                return json(array('code' => 2, 'msg' => '该友链已经提交过了，请不要重复提交哦！', 'data' => []));
            }
            $configPr = $this->siteConfig['apply_friendly_baidu_pr'];
            $prRes = Site::aizhanInfo($domain);
            if (@$prRes['data']) {
                if (empty($prRes['data'])) {
                    return json(array('code' => 2, 'msg' => '我太难了，居然在爱站上没抓取到您的站点，暂时无法收录，或者您也可以联系站长QQ人工收录一下！', 'data' => []));
                } else {
                    $seoData = $prRes['data'];
                    if ($seoData['baidu_pr'] < $configPr && $seoData['baidu_mpr'] < $configPr) {
                        return json(array('code' => 2, 'msg' => '你提交网站的爱站百度权重情况：PC端是' . $seoData['baidu_pr'] . '手机端是' . $seoData['baidu_mpr'] . '<br/>暂时不符合我们的收录要求~', 'data' => []));
                    }else{
                        $data['baidu_pr'] =$seoData['baidu_pr'];
                        $data['baidu_mpr'] =$seoData['baidu_mpr'];
                        $data['sogou_pr'] =$seoData['sogou_pr'];
                        $data['google_pr'] =$seoData['google_pr'];
                    }
                }
            }
            $where['poster_ip'] = $ip;
            $where['created_at'] = ['>', date('Y-m-d')];
            $count = (New FriendlyLink())->where($where)->count();
            if ($count >= 3) {
                return json(array('code' => 2, 'msg' => '你的热情有些泛滥，臣妾Hold不住呀？要不单独Q聊哦', 'data' => []));
            }
            $data['title'] = remove_xss($_data['name']);
            $data['poster_qq'] = remove_xss($_data['qq']);
            $data['link'] = remove_xss($_data['link']);
            $data['domain'] = $domain;
            $data['poster_ip'] = $ip;
            FriendlyLink::create($data);
            return json(array('code' => 1, 'msg' => '你的友链申请已成功提交，请耐心等待我们Q你并告知结果！', 'data' => []));
        } else {
            return view();
        }
    }


    public function tgrule(){
        return view();
    }

}
